<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 
        高度塌陷：
        当父元素的高度被子元素撑开时 如果子元素设置浮动将导致父元素高度丢失
        此现象称为高度塌陷
        
        
        */
        .box1 {
            width: 100%;
            border: 5px solid red;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: pink;
            float: left;
        }

        .box3 {
            width: 80%;
            height: 150px;
            background-color: yellow;
        }

        .box4 {
            width: 200px;
            height: 100px;
            background-color: green;
            margin-top: 50px;
            margin-left: 150px;
        }



        /* 解决高度塌陷 */
        /* .box1::after {
            content: "";
            display: block;
            clear: both;
        } */
        /* 解决垂直外边距重叠 */
        /*   .box3::before {
            content: "";
            display: table;
        } */

        /* 写一个既可以解决高度塌陷又可以解决垂直外边距重叠的class */
        .clearfix::after,
        .clearfix::before {
            content: "";
            display: table;
            clear: both;
        }
    </style>
</head>

<body>
    <div class="box1 clearfix">
        <div class="box2"></div>
        <!-- 添加一个空白的div清除浮动的影响可以解决 不推荐-->
        <!-- <div style="clear: both;"></div> -->
    </div>
    <hr>

    <div class="box3 clearfix">
        <div class="box4"></div>
    </div>


</body>

</html>